package io.renren.modules.sys.web_api;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;

import io.renren.common.utils.R;
import io.renren.modules.sys.back.entity.*;
import io.renren.modules.sys.back.service.*;

import io.renren.modules.sys.back.service.impl.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author goudandan
 * @email reszj1@gmail.com
 * @date 2018-09-12 16:49:25
 */
@Controller
@RequestMapping("api/crawler")
public class BtCrawlerController {
    @Autowired
    private BtCategroyManageService btCategroyManageService;
    private String crawlStorageFolder = "d:/crawl/test";
    private int numberOfCrawlers = 1;
    private static Long  Pcount =0L;
    @RequestMapping("/first")
    public String CrawlerFirst(){
        return "crawler2";
    }
    @RequestMapping("/fjs")
    public String CrawlerFirstfjs(){
        return "jishu";
    }
    @RequestMapping("/fpinpa")
    public String CrawlerFirstfpinpa(){
        return "pinpa";
    }
    @RequestMapping("/fwenda")
    public String CrawlerFirstfwenda(){
        return "wenda";
    }
    @RequestMapping("/start")
    public void CrawlerStart(String crawlurl)throws Exception{
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);
        /*
         * Instantiate the controller for this crawl.
         */
        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);


        controller.addSeed(crawlurl);
        Map<String,Object> param = new HashMap<>();
        param.put("param",crawlurl.split("/")[0]+"//"+crawlurl.split("/")[2]+"/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        controller.start(SimpleCrawlerImpl.class, numberOfCrawlers);

        Map<String,Object> cud =(Map<String,Object>) controller.getCustomData();
        if(cud.containsKey("par")){
            // 停止爬取
            controller.shutdown();
            System.out.println("停止爬取");

            // 等待结束任务
            controller.waitUntilFinish();
          //保存查到的数据
            BtCategroyManageEntity gmbean=(BtCategroyManageEntity) cud.get("par");
            BtCategroyManageEntity getbean = btCategroyManageService.getByname(gmbean.getName());
            if(getbean!=null){
                gmbean.setId(getbean.getId());
                getbean.setDeepnum(gmbean.getDeepnum());
                btCategroyManageService.updateById(getbean);
            }else {
                btCategroyManageService.insertOneBackprimkey(gmbean);
            }
           List<BtCategroyManageEntity> gmbeanlist=(List<BtCategroyManageEntity>) cud.get("sublist");
            for(BtCategroyManageEntity sb:gmbeanlist){
                BtCategroyManageEntity getsubbean = btCategroyManageService.getByname(sb.getName());
                if(getsubbean != null) continue;
                sb.setPid(gmbean.getId());
                btCategroyManageService.insertOrUpdate(sb);
            }

        }
    }
    @ResponseBody
    @RequestMapping("/detail")
    public R CrawlerStartdetail(String crawlurl)throws Exception {
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
       // crawlurl ="https://www.xianjichina.com/baike/detail_"+Pcount+".html";
        controller.addSeed(crawlurl);
        Map<String, Object> param = new HashMap<>();
        param.put("param", crawlurl.split("/")[0] + "//" + crawlurl.split("/")[2] + "/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        System.out.println("kaisu ________________________________+++++++++++++++++++++++++++");
        controller.start(SimpleDetailCrawlerImpl.class, numberOfCrawlers);

        Map<String, Object> cud = (Map<String, Object>) controller.getCustomData();
        if (cud.containsKey("back")) {
            Boolean bas = (Boolean) cud.get("back");
            controller.shutdown();
            System.out.println("停止爬取");
            // 等待结束任务
            controller.waitUntilFinish();
            if (bas == false) {
                return R.ok();
            } else {
                //保存查到的数据
                savedate(cud);
                return R.ok();
            }
        }
        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/renwu")
    public R Renwu(String crawlurl)throws Exception {
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
        // crawlurl ="https://www.xianjichina.com/baike/detail_"+Pcount+".html";
        controller.addSeed(crawlurl);
        Map<String, Object> param = new HashMap<>();
        param.put("param", crawlurl.split("/")[0] + "//" + crawlurl.split("/")[2] + "/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        System.out.println("kaisu ________________________________+++++++++++++++++++++++++++");
        controller.start(SimpleRenwuCrawlerImpl.class, numberOfCrawlers);

        Map<String, Object> cud = (Map<String, Object>) controller.getCustomData();
        if (cud.containsKey("back")) {
            Boolean bas = (Boolean) cud.get("back");
            controller.shutdown();
            System.out.println("停止爬取");
            // 等待结束任务
            controller.waitUntilFinish();
            if (bas == false) {
                return R.ok();
            } else {
                //保存查到的数据
               // savedate(cud);
                BtRenwuInfoEntity sb = new BtRenwuInfoEntity();
                if(cud.containsKey("name")){
                    sb.setName((String) cud.get("name"));
                }
                if(cud.containsKey("summer")){
                    sb.setSummer((String) cud.get("summer"));
                }
                if(cud.containsKey("content")){
                    sb.setContent((String) cud.get("content"));
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sb.setTisj(sdf.format(new Date()));
                sb.setLlcs((int)(Math.random()*1000));
                sb.setDzcs((int)(Math.random()*1000));
                sb.setTjr(1L);
                btRenwuInfoService.insertOrUpdate(sb);
                return R.ok();
            }
        }
        return R.ok();
    }
    @ResponseBody
    @RequestMapping("/jishu")
    public R jishu(String crawlurl)throws Exception {
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
        // crawlurl ="https://www.xianjichina.com/baike/detail_"+Pcount+".html";
        controller.addSeed(crawlurl);
        Map<String, Object> param = new HashMap<>();
        param.put("param", crawlurl.split("/")[0] + "//" + crawlurl.split("/")[2] + "/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        System.out.println("kaisu ____jishu______jishu______+++++++++++++++++++++++++++");
        controller.start(SimpleJishuCrawlerImpl.class, numberOfCrawlers);

        Map<String, Object> cud = (Map<String, Object>) controller.getCustomData();
        if (cud.containsKey("back")) {
            Boolean bas = (Boolean) cud.get("back");
            controller.shutdown();
            System.out.println("停止爬取");
            // 等待结束任务
            controller.waitUntilFinish();
            if (bas == false) {
                return R.ok();
            } else {
                //保存查到的数据
                // savedate(cud);
                BtJishuInfoEntity sb = new BtJishuInfoEntity();
                if(cud.containsKey("name")){
                    sb.setName((String) cud.get("name"));
                }
                if(cud.containsKey("summer")){
                    sb.setSummer((String) cud.get("summer"));
                }
                if(cud.containsKey("content")){
                    sb.setContent((String) cud.get("content"));
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sb.setTisj(sdf.format(new Date()));
                sb.setLlcs((int)(Math.random()*1000));
                sb.setDzcs((int)(Math.random()*1000));
                sb.setTjr(1L);
                btJishuInfoService.insertOrUpdate(sb);
                return R.ok();
            }
        }
        return R.ok();
    }
    @Autowired
    private BtJishuInfoService btJishuInfoService;
    @ResponseBody
    @RequestMapping("/pinpai")
    public R pinpai(String crawlurl)throws Exception {
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
        // crawlurl ="https://www.xianjichina.com/baike/detail_"+Pcount+".html";
        controller.addSeed(crawlurl);
        Map<String, Object> param = new HashMap<>();
        param.put("param", crawlurl.split("/")[0] + "//" + crawlurl.split("/")[2] + "/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        System.out.println("kaisu ____pinpai______pinpai______+++++++++++++++++++++++++++");
        controller.start(SimplePingpaiCrawlerImpl.class, numberOfCrawlers);

        Map<String, Object> cud = (Map<String, Object>) controller.getCustomData();
        if (cud.containsKey("back")) {
            Boolean bas = (Boolean) cud.get("back");
            controller.shutdown();
            System.out.println("停止爬取");
            // 等待结束任务
            controller.waitUntilFinish();
            if (bas == false) {
                return R.ok();
            } else {
                //保存查到的数据
                // savedate(cud);
                BtPingpaiInfoEntity sb = new BtPingpaiInfoEntity();
                if(cud.containsKey("name")){
                    sb.setName((String) cud.get("name"));
                }
                if(cud.containsKey("summer")){
                    sb.setSummer((String) cud.get("summer"));
                }
                if(cud.containsKey("content")){
                    sb.setContent((String) cud.get("content"));
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sb.setTisj(sdf.format(new Date()));
                sb.setLlcs((int)(Math.random()*1000));
                sb.setDzcs((int)(Math.random()*1000));
                sb.setTjr(1L);
                btPingpaiInfoService.insertOrUpdate(sb);
                return R.ok();
            }
        }
        return R.ok();
    }
    @Autowired
    private BtPingpaiInfoService btPingpaiInfoService;
    @ResponseBody
    @RequestMapping("/wenda")
    public R wenda(String crawlurl)throws Exception {
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
        config.setMaxDepthOfCrawling(0);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
        // crawlurl ="https://www.xianjichina.com/baike/detail_"+Pcount+".html";
        controller.addSeed(crawlurl);
        Map<String, Object> param = new HashMap<>();
        param.put("param", crawlurl.split("/")[0] + "//" + crawlurl.split("/")[2] + "/");
        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.setCustomData(param);
        System.out.println("kaisu ____wenda______wenda______+++++++++++++++++++++++++++");
        controller.start(SimplewendaCrawlerImpl.class, numberOfCrawlers);

        Map<String, Object> cud = (Map<String, Object>) controller.getCustomData();
        if (cud.containsKey("back")) {
            Boolean bas = (Boolean) cud.get("back");
            controller.shutdown();
            System.out.println("停止爬取");
            // 等待结束任务
            controller.waitUntilFinish();
            if (bas == false) {
                return R.ok();
            } else {
                //保存查到的数据
                // savedate(cud);
                BtWendaInfoEntity sb = new BtWendaInfoEntity();
                if(cud.containsKey("name")){
                    sb.setName((String) cud.get("name"));
                }
                if(cud.containsKey("summer")){
                    sb.setSummer((String) cud.get("summer"));
                }
                if(cud.containsKey("content")){
                    sb.setContent((String) cud.get("content"));
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sb.setTisj(sdf.format(new Date()));
                sb.setLlcs((int)(Math.random()*1000));
                sb.setDzcs((int)(Math.random()*1000));
                sb.setTjr(1L);
                btWendaInfoService.insertOrUpdate(sb);
                return R.ok();
            }
        }
        return R.ok();
    }
    @Autowired
    private BtWendaInfoService btWendaInfoService;
    @Autowired
    private BtRenwuInfoService btRenwuInfoService;
    private void savedate( Map<String, Object> cud){
        //添加主分类
        List<String> zhufen = (List<String>)cud.get("zhufen");
        BtCategroyManageEntity zflp0 = btCategroyManageService.getByname(zhufen.get(0));
        Long SavefenleId =0L;
        if(zhufen.size()<3) return;
        if(zflp0 != null){
            List<BtCategroyManageEntity> zflp2list = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",zflp0.getId()));
            Boolean isinp2=false;
            BtCategroyManageEntity tempb=null;
            for(BtCategroyManageEntity sb:zflp2list){
                if(sb.getName().equals(zhufen.get(1))){
                    isinp2=true;
                    tempb = sb;
                    break;
                }
            }
            if(isinp2){
                Boolean isinp3=false;
                List<BtCategroyManageEntity> zflp3list = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",tempb.getId()));
                for(BtCategroyManageEntity sb:zflp3list){
                    if(sb.getName().equals(zhufen.get(2))){
                        isinp3=true;
                        tempb = sb;
                        break;
                    }
                }
                if(!isinp3){//不在zai
                    BtCategroyManageEntity svb = new BtCategroyManageEntity();
                    svb.setName(zhufen.get(2));
                    svb.setPid(tempb.getId());
                    btCategroyManageService.insertOneBackprimkey(svb);
                    SavefenleId =svb.getId();
                }
            }else{//erji bucunzai
                BtCategroyManageEntity svb = new BtCategroyManageEntity();
                svb.setName(zhufen.get(1));
                svb.setPid(zflp0.getId());
                btCategroyManageService.insertOneBackprimkey(svb);
                BtCategroyManageEntity svb2 = new BtCategroyManageEntity();
                svb2.setName(zhufen.get(2));
                svb2.setPid(svb.getId());
                btCategroyManageService.insertOneBackprimkey(svb2);
                SavefenleId =svb2.getId();

            }
        }else{//
            BtCategroyManageEntity svb = new BtCategroyManageEntity();
            svb.setName(zhufen.get(0));
            svb.setPid(0L);
            btCategroyManageService.insertOneBackprimkey(svb);
            BtCategroyManageEntity svb1 = new BtCategroyManageEntity();
            svb1.setName(zhufen.get(1));
            svb1.setPid(svb.getId());
            btCategroyManageService.insertOneBackprimkey(svb1);
            BtCategroyManageEntity svb2 = new BtCategroyManageEntity();
            svb2.setName(zhufen.get(2));
            svb2.setPid(svb1.getId());
            btCategroyManageService.insertOneBackprimkey(svb2);
            SavefenleId =svb2.getId();
        }
        //保存内容
        BtCtBaseEntity btCtBaseEntity = new BtCtBaseEntity();
        btCtBaseEntity.setName(zhufen.get(2));
        btCtBaseEntity.setFid(SavefenleId);
        btCtBaseService.insertOneBackprimkey(btCtBaseEntity);
        //概述
        String gaishu = (String) cud.get("gaishu");
        BtCtGaishuEntity btCtGaishuEntity = new BtCtGaishuEntity();
        btCtGaishuEntity.setBid(btCtBaseEntity.getId());
        btCtGaishuEntity.setContent(gaishu);
        btCtGaishuService.insertOneBackprimkey(btCtGaishuEntity);
        btCtBaseEntity.setGsid(btCtGaishuEntity.getId());
        //工作原理
        String yuanli = (String) cud.get("yuanli");
        BtCtGongzhuoyuanliEntity btCtGongzhuoyuanliEntity = new BtCtGongzhuoyuanliEntity();
        btCtGongzhuoyuanliEntity.setBid(btCtBaseEntity.getId());
        btCtGongzhuoyuanliEntity.setContent(yuanli);
        btCtGongzhuoyuanliService.insertOneBackprimkey(btCtGongzhuoyuanliEntity);
        btCtBaseEntity.setGzylid(btCtGongzhuoyuanliEntity.getId());

        //获取应用
        Map<String,Object> yongtu = (Map<String,Object>) cud.get("yongtu");
        if(yongtu!=null){
            BtCtYingyongEntity btCtYingyongEntity = new BtCtYingyongEntity();
            if(yongtu.containsKey("summary")){
                btCtYingyongEntity.setSummary((String)yongtu.get("summary"));
            }
            if(yongtu.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) yongtu.get("subname");
                List<String> tsubclist = (List<String>) yongtu.get("subcontent");
                for(String ss :tlist){
                    BtCtYingyongDetailEntity btCtYingyongDetailEntity = new BtCtYingyongDetailEntity();
                    btCtYingyongDetailEntity.setName(ss);
                    btCtYingyongDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtYingyongDetailServiceImpl.insertOneBackprimkey(btCtYingyongDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtYingyongDetailEntity.getId();
                    else
                        feleiid += btCtYingyongDetailEntity.getId()+";";
                }
                btCtYingyongEntity.setFenleiid(feleiid);
            }
            btCtYingyongEntity.setBid(btCtBaseEntity.getId());
            btCtYingyongService.insertOneBackprimkey(btCtYingyongEntity);
            btCtBaseEntity.setYyid(btCtYingyongEntity.getId());
        }
        //分类的存储
        //分类
        Map<String,Object> fenlei = (Map<String,Object>) cud.get("fenlei");
        if(fenlei!=null){
            BtCtFenleiEntity btCtFenleiEntity = new BtCtFenleiEntity();
            if(fenlei.containsKey("summary")){
                btCtFenleiEntity.setSummary((String)fenlei.get("summary"));
            }
            if(fenlei.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) fenlei.get("subname");
                List<String> tsubclist = (List<String>) fenlei.get("subcontent");
                for(String ss :tlist){
                    BtCtFenleiDetailEntity btCtFenleiDetailEntity = new BtCtFenleiDetailEntity();
                    btCtFenleiDetailEntity.setName(ss);
                    btCtFenleiDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtFenleiDetailService.insertOneBackprimkey(btCtFenleiDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtFenleiDetailEntity.getId();
                    else
                        feleiid += btCtFenleiDetailEntity.getId()+";";
                }
                btCtFenleiEntity.setFenleiid(feleiid);
            }
            btCtFenleiEntity.setBid(btCtBaseEntity.getId());
            btCtFenleiService.insertOneBackprimkey(btCtFenleiEntity);
            btCtBaseEntity.setFlid(btCtFenleiEntity.getId());
        }

        //注意事项
        Map<String,Object> zhuyi = (Map<String,Object>) cud.get("zhuyi");
        if(zhuyi!=null){
            BtCtZhuyishixiangEntity btCtZhuyishixiangEntity = new BtCtZhuyishixiangEntity();
            if(zhuyi.containsKey("summary")){
                btCtZhuyishixiangEntity.setSummary((String)zhuyi.get("summary"));
            }
            if(zhuyi.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) zhuyi.get("subname");
                List<String> tsubclist = (List<String>) zhuyi.get("subcontent");
                for(String ss :tlist){
                    BtCtZhuyishixiangDetailEntity btCtZhuyishixiangDetailEntity = new BtCtZhuyishixiangDetailEntity();
                    btCtZhuyishixiangDetailEntity.setName(ss);
                    btCtZhuyishixiangDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtZhuyishixiangDetailService.insertOneBackprimkey(btCtZhuyishixiangDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtZhuyishixiangDetailEntity.getId();
                    else
                        feleiid += btCtZhuyishixiangDetailEntity.getId()+";";
                }
                btCtZhuyishixiangEntity.setFenleiid(feleiid);
            }
            btCtZhuyishixiangEntity.setBid(btCtBaseEntity.getId());
            btCtZhuyishixiangService.insertOneBackprimkey(btCtZhuyishixiangEntity);
            btCtBaseEntity.setZysxid(btCtZhuyishixiangEntity.getId());
        }

        //选型指南
        Map<String,Object> zhinan = (Map<String,Object>) cud.get("zhinan");
        if(zhinan!=null){
            BtCtXuanxingzhinanEntity btCtXuanxingzhinanEntity = new BtCtXuanxingzhinanEntity();
            if(zhinan.containsKey("summary")){
                btCtXuanxingzhinanEntity.setSummary((String)zhinan.get("summary"));
            }
            if(zhinan.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) zhinan.get("subname");
                List<String> tsubclist = (List<String>) zhinan.get("subcontent");
                for(String ss :tlist){
                    BtCtXuanxingzhinanDetailEntity btCtXuanxingzhinanDetailEntity = new BtCtXuanxingzhinanDetailEntity();
                    btCtXuanxingzhinanDetailEntity.setName(ss);
                    btCtXuanxingzhinanDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtXuanxingzhinanDetailService.insertOneBackprimkey(btCtXuanxingzhinanDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtXuanxingzhinanDetailEntity.getId();
                    else
                        feleiid += btCtXuanxingzhinanDetailEntity.getId()+";";
                }
                btCtXuanxingzhinanEntity.setFenleiid(feleiid);
            }
            btCtXuanxingzhinanEntity.setBid(btCtBaseEntity.getId());
            btCtXuanxingzhinanService.insertOneBackprimkey(btCtXuanxingzhinanEntity);
            btCtBaseEntity.setXxznid(btCtXuanxingzhinanEntity.getId());
        }

        //维护保养
        Map<String,Object> baoyang = (Map<String,Object>) cud.get("baoyang");
        if(baoyang!=null){
            BtCtWeihubaoyangEntity btCtWeihubaoyangEntity = new BtCtWeihubaoyangEntity();
            if(baoyang.containsKey("summary")){
                btCtWeihubaoyangEntity.setSummary((String)baoyang.get("summary"));
            }
            if(baoyang.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) baoyang.get("subname");
                List<String> tsubclist = (List<String>) baoyang.get("subcontent");
                for(String ss :tlist){
                    BtCtWeihubaoyangDetailEntity btCtWeihubaoyangDetailEntity = new BtCtWeihubaoyangDetailEntity();
                    btCtWeihubaoyangDetailEntity.setName(ss);
                    btCtWeihubaoyangDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtWeihubaoyangDetailService.insertOneBackprimkey(btCtWeihubaoyangDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtWeihubaoyangDetailEntity.getId();
                    else
                        feleiid += btCtWeihubaoyangDetailEntity.getId()+";";
                }
                btCtWeihubaoyangEntity.setFenleiid(feleiid);
            }
            btCtWeihubaoyangEntity.setBid(btCtBaseEntity.getId());
            btCtWeihubaoyangService.insertOneBackprimkey(btCtWeihubaoyangEntity);
            btCtBaseEntity.setWhbyid(btCtWeihubaoyangEntity.getId());
        }
        //故障排除
        Map<String,Object> guzhang = (Map<String,Object>) cud.get("guzhang");
        if(guzhang!=null){
            BtCtGuozhangpaichuEntity btCtGuozhangpaichuEntity = new BtCtGuozhangpaichuEntity();
            if(guzhang.containsKey("summary")){
                btCtGuozhangpaichuEntity.setSummary((String)guzhang.get("summary"));
            }
            if(guzhang.containsKey("subname")){
                String feleiid = "";
                List<String> tlist = (List<String>) guzhang.get("subname");
                List<String> tsubclist = (List<String>) guzhang.get("subcontent");
                for(String ss :tlist){
                    BtCtGuozhangpaichuDetailEntity btCtGuozhangpaichuDetailEntity = new BtCtGuozhangpaichuDetailEntity();
                    btCtGuozhangpaichuDetailEntity.setName(ss);
                    btCtGuozhangpaichuDetailEntity.setContent(tsubclist.get(tlist.indexOf(ss)));
                    btCtGuozhangpaichuDetailService.insertOneBackprimkey(btCtGuozhangpaichuDetailEntity);
                    if(tlist.indexOf(ss)+1 == tlist.size())
                        feleiid += btCtGuozhangpaichuDetailEntity.getId();
                    else
                        feleiid += btCtGuozhangpaichuDetailEntity.getId()+";";
                }
                btCtGuozhangpaichuEntity.setFenleiid(feleiid);
            }
            btCtGuozhangpaichuEntity.setBid(btCtBaseEntity.getId());
            btCtGuozhangpaichuService.insertOneBackprimkey(btCtGuozhangpaichuEntity);
            btCtBaseEntity.setGzpcid(btCtGuozhangpaichuEntity.getId());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        btCtBaseEntity.setTjsj(sdf.format(new Date()));
        btCtBaseEntity.setLlcs((int)(Math.random()*1000));
        btCtBaseEntity.setDzcs((int)(Math.random()*1000));
        btCtBaseEntity.setCjr(1L);
        btCtBaseService.insertOrUpdate(btCtBaseEntity);
    }

    @Autowired
    private BtCtBaseService btCtBaseService;
    @Autowired
    private BtCtGaishuService btCtGaishuService;
    @Autowired
    private BtCtGongzhuoyuanliService btCtGongzhuoyuanliService;
    @Autowired
    private BtCtYingyongService btCtYingyongService;
    @Autowired
    private BtCtYingyongDetailServiceImpl btCtYingyongDetailServiceImpl;
    @Autowired
    private BtCtFenleiService btCtFenleiService;
    @Autowired
    private BtCtFenleiDetailService btCtFenleiDetailService;
    @Autowired
    private BtCtZhuyishixiangService btCtZhuyishixiangService;
    @Autowired
    private BtCtZhuyishixiangDetailService btCtZhuyishixiangDetailService;

    @Autowired
    private BtCtXuanxingzhinanService btCtXuanxingzhinanService;
    @Autowired
    private BtCtXuanxingzhinanDetailService btCtXuanxingzhinanDetailService;

    @Autowired
    private BtCtWeihubaoyangService btCtWeihubaoyangService;
    @Autowired
    private BtCtWeihubaoyangDetailService btCtWeihubaoyangDetailService;

    @Autowired
    private BtCtGuozhangpaichuService btCtGuozhangpaichuService;
    @Autowired
    private BtCtGuozhangpaichuDetailService btCtGuozhangpaichuDetailService;
}
